﻿using System;
using System.Windows;
using System.Windows.Input;
using Sonce.Editor.Tools;

namespace Sonce.Editor
{
	public partial class Editor
	{
		#region Event handlers
		protected void Editor_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			//If any modifier is pressed use alternative tool
			//if ((Keyboard.Modifiers & ~ModifierKeys.None) != 0)
			//{
			//    #region Alternative tool                
			//    if (this.alternativeTool is IRawTool)
			//    {
			//        ((IRawTool)this.alternativeTool).MouseDown(sender, e);
			//    }
			//    else if (this.alternativeTool is IEditorTool)
			//    {
			//        ((IEditorTool)this.alternativeTool).MouseDown(sender, e);
			//    }
			//    #endregion
			//}
			//else

			this.isMousePressed = true;
			if (sender != null && sender is UIElement)
			{
				this.startPoint = e.GetPosition((UIElement)sender);
				if (this.SnapToGrid && (!(this.SelectedTool is HandTool) || this.IsHoldingNode))
				{
					this.startPoint.X = (double)(Math.Round(startPoint.X / this.SnapRange)) * this.SnapRange;
					this.startPoint.Y = (double)(Math.Round(startPoint.Y / this.SnapRange)) * this.SnapRange;
				}

				bool handled = false;
				if (!handled && this.SelectedTool is IRawTool)
				{
					handled = ((IRawTool)this.SelectedTool).MouseDown(sender, e);
				}
				if (!handled && this.SelectedTool is IEditorTool)
				{
					handled = ((IEditorTool)this.SelectedTool).MouseDown(this.startPoint);
				}
			}
		}

		protected void Editor_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{
			//If any modifier is pressed use alternative tool
			//if ((Keyboard.Modifiers & ~ModifierKeys.None) != 0 && this.viewTool != null)
			//{
			//    #region ViewTool
			//    this.startPoint = e.GetPosition(this.Parent as FrameworkElement);
			//    this.viewTool.MouseDown(this.startPoint);
			//    #endregion
			//}
			//else

			this.isMousePressed = false;
			if (sender != null && sender is UIElement)
			{
				this.endPoint = e.GetPosition((UIElement)sender);
				if (this.SnapToGrid && (!(this.SelectedTool is HandTool) || this.IsHoldingNode))
				{
					this.endPoint.X = (double)(Math.Round(endPoint.X / this.SnapRange)) * this.SnapRange;
					this.endPoint.Y = (double)(Math.Round(endPoint.Y / this.SnapRange)) * this.SnapRange;
				}

				bool handled = false;
				if (!handled && this.SelectedTool is IRawTool)
				{
					handled = ((IRawTool)this.SelectedTool).MouseUp(sender, e);
				}
				if (!handled && this.SelectedTool is IEditorTool)
				{
					handled = ((IEditorTool)this.SelectedTool).MouseUp(this.endPoint);
				}
			}
		}

		protected void Editor_MouseMove(object sender, MouseEventArgs e)
		{
			//If any modifier is pressed use alternative tool
			//if ((Keyboard.Modifiers & ~ModifierKeys.None) != 0 && this.viewTool != null)
			//{
			//    #region ViewTool
			//    this.startPoint = e.GetPosition(this.Parent as FrameworkElement);
			//    this.viewTool.MouseDown(this.startPoint);
			//    #endregion
			//}
			//else

			if (sender != null && sender is UIElement)
			{
				this.movePoint = e.GetPosition((UIElement)sender);
				if (this.SnapToGrid && (!(this.SelectedTool is HandTool) || this.IsHoldingNode))
				{
					this.movePoint.X = (double)(Math.Round(movePoint.X / this.SnapRange)) * this.SnapRange;
					this.movePoint.Y = (double)(Math.Round(movePoint.Y / this.SnapRange)) * this.SnapRange;
				}

				this.LayerPreview.Clear();
				this.LayerCursor.DrawCursor(this.movePoint, this.CustomCursor);

				bool handled = false;
				if (!handled && this.SelectedTool is IRawTool)
				{
					handled = ((IRawTool)this.SelectedTool).MouseMove(sender, e);
				}
				if (!handled && this.SelectedTool is IEditorTool)
				{
					handled = ((IEditorTool)this.SelectedTool).MouseMove(this.movePoint, this.isMousePressed);
				}
			}
		}

		protected void Editor_MouseEnter(object sender, MouseEventArgs e)
		{
			bool handled = false;
			if (!handled && this.SelectedTool is IRawTool)
			{
				handled = ((IRawTool)this.SelectedTool).MouseEnter(sender, e);
			}
			if (!handled && this.SelectedTool is IEditorTool)
			{
				handled = ((IEditorTool)this.SelectedTool).MouseEnter();
			}
		}

		protected void Editor_MouseLeave(object sender, MouseEventArgs e)
		{
			bool handled = false;
			if (!handled && this.SelectedTool is IRawTool)
			{
				handled = ((IRawTool)this.SelectedTool).MouseLeave(sender, e);
			}
			if (!handled && this.SelectedTool is IEditorTool)
			{
				handled = ((IEditorTool)this.SelectedTool).MouseLeave();
			}
			//else
			//{
			//    //Mouse moved out of the editor.
			//    //ReleaseTool();
			//}
		}

		protected void Editor_LostMouseCapture(object sender, MouseEventArgs e)
		{
			bool handled = false;
			if (!handled && this.SelectedTool is IRawTool)
			{
				handled = ((IRawTool)this.SelectedTool).MouseLeave(sender, e);
			}
			if (!handled && this.SelectedTool is IEditorTool)
			{
				handled = ((IEditorTool)this.SelectedTool).MouseLeave();
			}
			//else
			//{
			//    //Other application took mouse event.
			//    //ReleaseTool();
			//}
		}

		protected void Helper_MouseWheelChanged(object sender, EventArgs<int> e)
		{
			bool handled = false;
			if (!handled && this.SelectedTool is IRawTool)
			{
				handled = ((IRawTool)this.SelectedTool).MouseWheel(sender, e);
			}
			if (!handled && this.SelectedTool is IEditorTool)
			{
				handled = ((IEditorTool)this.SelectedTool).MouseWheel(e.Result);
			}
		}

		protected void LayoutRoot_KeyDown(object sender, KeyEventArgs e)
		{
			KeyDown(sender, e);
		}

		protected void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
		{
			KeyUp(sender, e);
		}

		protected void LayoutRoot_SizeChanged(object sender, SizeChangedEventArgs e)
		{
			//Refresh grid
			layerGrid.Clear();
			layerGrid.DrawGrid();
		}

		protected void layerCircuit_VisualObjectDrawn(object sender, LayerEventArgs e)
		{
			//if (e.Element.Tag is ITag)
			//{
			//    if (e.Element.Tag is ElementTag)
			//    {
			//    }
			//    else if (e.Element.Tag is NodeTag)
			//    {
			//    }
			//}
			//this.logicTree.Elements.Add(e.Element);
		}
		#endregion
	}
}
